package com.ysd.Dao.Impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.Banner;
import com.ysd.entity.NewsClassiIfication;
import com.ysd.util.DBmanager;

public class BannerDaoImpl {
	/**
	 * 分页查询轮播图
	 */
	public List<Banner> getBannerByPage(int page,int size) {
		List<Banner> list = new ArrayList<Banner>();
		Connection conn = DBmanager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from banner order by bannerId desc limit "+page+","+size);
			while(rs.next()) {
				int bannerId = rs.getInt("bannerId");
				String  bannerPicture = rs.getString("bannerPicture");
				int bannerCategory = rs.getInt("bannerCategory");
				Banner  banner=  new Banner(bannerId,bannerPicture,bannerCategory);
				list.add(banner);	
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBmanager.closeAll(conn, st, rs);
		}
	}
	
	
	
	/**
	 * 查询所有轮播图数量
	 */
	public List<Banner> getBanner() {
		List<Banner> list = new ArrayList<Banner>();
		Connection conn = DBmanager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from banner");
			while(rs.next()) {
				int bannerId = rs.getInt("bannerId");
				String  bannerPicture = rs.getString("bannerPicture");
				int bannerCategory = rs.getInt("bannerCategory");
				Banner  banner=  new Banner(bannerId,bannerPicture,bannerCategory);
				list.add(banner);	
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBmanager.closeAll(conn, st, rs);
		}
	}
	/**
	 * 
	 * 根据轮播id删除轮播图
	 * bId前台传来的参数
	 * @author 86176
	 *return 返回受影响行数
	 */
	public int deleteBannerById(int bId) {
		return DBmanager.executeUpdate("DELETE FROM banner where bannerId ="+bId);
	}
	/**
	 * 
	 * 根据轮播id批量删除轮播图
	 * bId前台传来的参数
	 * @author 86176
	 *return 返回受影响行数
	 */
	public int deleteBannerByIds(String bIds) {
		return DBmanager.executeUpdate("DELETE FROM banner where bannerId in("+bIds+")");
	}
	/**
	   *添加轮播图
	 * ba 前台传来的轮播数据
	 *return 受影响行数
	 */
	public int addBanner(Banner ba) {
		return DBmanager.executeUpdate("insert into banner VALUES(null,'"+ba.getBannerPicture()+"','"+ba.getBannerCategory()+"')");
	}
	/**
	 * 
	 * 根据轮播id查询轮播图
	 * bId前台传来的参数
	 * @author 86176
	 */
	public Banner getBannerById(int bId) {
		Connection conn = DBmanager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		Banner banner = null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from banner where bannerId="+bId);
			while(rs.next()) {
				int bannerId = rs.getInt("bannerId");
				String  bannerPicture = rs.getString("bannerPicture");
				int bannerCategory = rs.getInt("bannerCategory");
				banner=  new Banner(bannerId,bannerPicture,bannerCategory);
			}
			return banner;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBmanager.closeAll(conn, st, rs);
		}
	}
	
	
	/**
	 *编辑轮播图
	 * ba 前台传来产品分类数据
	 *return 受影响行数
	 */
	public int editBanner(Banner ba) {
		return DBmanager.executeUpdate("update banner set bannerPicture = '"+ba.getBannerPicture()+"' ,  bannerCategory = '"+ba.getBannerCategory()+"' where bannerId='"+ba.getBannerId()+"' ");
	}
}
